<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['quality:defectiveItem:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['quality:defectiveItem:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['quality:defectiveItem:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#defectiveItem', '品检不良项目')"
          v-hasPermi="['quality:defectiveItem:export']"
        >
          导出
        </el-button>
      </el-col>

      <!-- 分页效果 -->
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      id="defectiveItem"
      highlight-current-row
      border
      stripe
      :height="tableH"
      v-loading="loading"
      :data="defectiveItemList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column
        fixed="left"
        label="#"
        type="index"
        width="40"
        align="center"
      />
      <el-table-column
        fixed="left"
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="不良代码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bllYydm"
      />
      <el-table-column
        label="不良描述"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bllYymc"
      />
      <el-table-column
        label="有效标识"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bllFlag"
      >
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.bllFlag"
            active-value="1"
            inactive-value="0"
            @change="changeFlag(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bllJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="bllJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.bllJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更改人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bllGgry"
      />
      <el-table-column
        label="更改日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="bllGgrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.bllGgrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['quality:defectiveItem:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['quality:defectiveItem:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="400px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-col :span="24">
          <el-form-item label="不良代码" prop="bllYydm">
            <el-input
              v-model="queryParams.bllYydm"
              placeholder="请输入不良代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="不良描述" prop="bllYymc">
            <el-input
              v-model="queryParams.bllYymc"
              placeholder="请输入不良描述"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改品检不良项目对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="不良代码" prop="bllYydm">
          <el-input v-model="form.bllYydm" placeholder="请输入不良代码" />
        </el-form-item>
        <el-form-item label="不良描述" prop="bllYymc">
          <el-input v-model="form.bllYymc" placeholder="请输入不良描述" />
        </el-form-item>
        <el-form-item label="有效标识" prop="bllFlag">
          <el-radio-group v-model="form.bllFlag">
            <el-radio label="1">有效</el-radio>
            <el-radio label="0">无效</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDefectiveItem,
  getDefectiveItem,
  delDefectiveItem,
  addDefectiveItem,
  updateDefectiveItem,
  changedDefectiveItemFlag,
} from "@/api/quality/defectiveItem";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "DefectiveItem",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      tableH: document.body.clientHeight - 320,
      // 品检不良项目表格数据
      defectiveItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
        bllYydm: null,
        bllYymc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bllYydm: [{ required: true, message: "不良代码必填", trigger: "blur" }],
        bllYymc: [{ required: true, message: "不良描述必填", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询品检不良项目列表 */
    getList() {
      this.loading = true;
      listDefectiveItem(this.queryParams).then((response) => {
        this.defectiveItemList = [];
        this.defectiveItemList = this.defectiveItemList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bllId: null,
        bllYydm: null,
        bllYymc: null,
        bllPdfl: null,
        bllFlag: "1",
        bllJlry: null,
        bllJlrq: null,
        bllGgry: null,
        bllGgrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bllYydm);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加品检不良项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bllYydm = row.bllYydm || this.ids;
      getDefectiveItem(bllYydm).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改品检不良项目";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询品检不良项目";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bllId != null) {
            updateDefectiveItem(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDefectiveItem(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bllYydms = row.bllYydm || this.ids;
      this.$confirm(
        '是否确认删除品检不良项目编号为"' + bllYydms + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delDefectiveItem(bllYydms);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 更改状态标识 **/
    changeFlag(row) {
      let text = row.bllFlag === "1" ? "启用" : "停用";
      this.$confirm("确认要" + text + "吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return changedDefectiveItemFlag(row.bllYydm, row.bllFlag);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
          this.getList();
        })
        .catch(function () {
          row.bllFlag = row.bllFlag === "0" ? "1" : "0";
        });
    },
    /** 导出按钮操作 */
    handleExport(a, b) {
      exportTable(a, b);
    },
    /** 分页 **/
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
